package ru.jarda.model.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.security.core.context.SecurityContextHolder;
import ru.jarda.model.entities.Category;
import ru.jarda.model.entities.CustomUser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by User on 31.05.2015.
 */
public class CategoriesDaoJdbcImpl  extends JdbcDaoSupport implements Dao<Category> {

    private String categoryType;

    public void setCategoryType(String categoryType) {
        this.categoryType = categoryType;
    }

    @Override
    public Category getObject (Number id) {
        String sql = "SELECT * FROM " + categoryType + "_categories WHERE id=?";
        String name;
        long parentId;
        int order;

        Map<String, Object> row = getJdbcTemplate().queryForMap(sql, id);
        name = (String)row.get("name");
        parentId = (long)row.get("parent_id");
        order = (int)row.get("order_in");

        return new Category((long)id, name, parentId, order);

    }

    @Override
    public void deleteObject(Number id) {
        getJdbcTemplate().update("DELETE FROM " + categoryType + "_categories WHERE id ="+id);
    }

    @Override
    public void saveObject(Category category) {
        CustomUser user = (CustomUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        String sql = "insert into " + categoryType +
                "_categories (name, parent_id, order_in, user_id) " +
                "values (?, ?, ?, ?)";
        getJdbcTemplate().update(sql,
            category.getName(), category.getParentId(),category.getOrder(),user.getUserID());

    }

@Override
    public void editObject(Category category) {

        long id=category.getId();
        String name=category.getName();
        long parentId = category.getParentId();
        int order = category.getOrder();
        String sql=  "UPDATE " + categoryType + "_categories SET name=?, parent_id=?, order_in=? WHERE id=?";
        getJdbcTemplate().update(sql, name, parentId, order, id);
    }

    @Override
    public List<Category> getAllObjects() {
        CustomUser user = (CustomUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        String sql = "SELECT * FROM " + categoryType + "_categories WHERE user_id=?";

        List<Category> categories = new ArrayList<>();
        long id;
        String name;
        long parentId;
        int order;

        List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql,user.getUserID());

        for (Map row : rows) {
            id = (long)row.get("id");
            name = (String)row.get("name");
            parentId = (long)row.get("parent_id");
            order = (int)row.get("order_in");
            categories.add(new Category(id, name, parentId, order));
        }

        return categories;
    }

}

